#include <Arduino.h>
#include "com/communication.h"
#include "message.h"
#include "protocol.h"



volatile bool button_pressed = false;
extern volatile bool received_msg;

void pair_with_master();

int main(void) {
	init();

	setup();

	for (;;)
		loop();

	return 0;
}



void button_interrupt() {
	button_pressed = true;
}

void setup() {
	//pinMode(2, INPUT); // RF24 Interrupt pin
	pinMode(3, INPUT); // Button Interrupt pin
	digitalWrite(3, HIGH);

	Serial.begin(9600);
	Serial.println("I\'m a slave!");

	attachInterrupt(1, button_interrupt, FALLING);

	com_activate_debug();
	com_init_slave();

	delay(500);
}

void loop() {
	//com_send_msg_to_master(message(0, PAIRING));
	/*if(com_available()) {
		Serial.println("Message available!");
		message msg = com_read_msg_from_master();
		if(msg.getComType() != ERROR) {
			Serial.println("Message received");
		}
	} else {
		Serial.println("No msg");
	}*/
	delay(100);
	if(button_pressed) { // Pairing
		Serial.println("Start paring...");
		pair_with_master();
		button_pressed = false;
	}
	/*
	if(received_msg) {
		received_msg = false;
	}
}

void pair_with_master() {
	com_pair_with_master();
}
